package cn.kimoon.dau_android;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.kimoon.dau_android.Model.ModelCollectorPower;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by xiangdong on 2016/11/23.
 */

public class ElectricityFragment extends Fragment {
    private ModelCollectorPower power;
    private TextView textView;
    private ImageView elecImageView;

    public void setPower(ModelCollectorPower power) {
        this.power = power;

        refreshWholeView();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_electricity, container, false);
        initSubViews(view);
        return view;
    }

    private void initSubViews(View view) {
        textView = (TextView) view.findViewById(R.id.power_text_view);
        elecImageView = (ImageView) view.findViewById(R.id.electricity_image_view);

//        refreshWholeView();
    }

    private void refreshWholeView() {
        if (elecImageView != null && textView != null) {
            if (power.pic != null && power.pic.length() != 0) {
                elecImageView.setVisibility(View.VISIBLE);

                byte[] decodeString = Base64.decode(power.pic, Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(decodeString, 0, decodeString.length);
                elecImageView.setImageBitmap(bitmap);
            } else {
                elecImageView.setVisibility(View.INVISIBLE);
            }

            String valueStr = String.valueOf(power.value);
            String string = valueStr + "kWh";
            SpannableString span = new SpannableString(string);
            span.setSpan(new AbsoluteSizeSpan(35, true), 0, valueStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            span.setSpan(new AbsoluteSizeSpan(20, true), valueStr.length(), string.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(span);
        }
    }
}
